/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar;

import com.xuexiang.xui_lib.component.toast.XToast;
import com.xuexiang.xui_lib.tabbar.JPTabBar;
import com.xuexiang.xui_lib.tabbar.tablayout.jptabbar.DataItem;
import com.xuexiang.xui_lib.tabbar.tablayout.jptabbar.PagerJptab;
import com.xuexiang.xui_lib.util.Log;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.XToastUtils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.PageSlider;
import ohos.agp.utils.LayoutAlignment;

import java.util.ArrayList;

/**
 * JPTabBar
 *
 * @since 2021-04-17
 */
public class JPTabBarSlice extends AbilitySlice {
    private static final int Y_OFFSET = 120;
    private static final String TAG = "AbilitySlice";
    private PageSlider pager;
    private JPTabBar tabBar;
    private String[] mtitle;
    private int tabmiddleView = ResourceTable.Layout_jptabbar_middle_view;
    private Image jptabback;

    private boolean isFirst = true;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_jptabbar);
        mtitle = new String[]{"信息", "报告", "活动", "设置"};
        XToast.Config.get()
            // 位置设置为居中
            .setGravity(LayoutAlignment.BOTTOM)
            .setYOffset(Y_OFFSET);
        initComponent();
        initpager();
        jptabback.setClickedListener(component -> terminateAbility());
    }

    private void initpager() {
        pager.setProvider(new PagerJptab(getData()));
    }

    private ArrayList<DataItem> getData() {
        ArrayList<DataItem> dataItem = new ArrayList<>();
        for (int index = 0; index < mtitle.length; index++) {
            dataItem.add(new DataItem("这个是" + mtitle[index] + "页面的内容"));
        }
        return dataItem;
    }

    private void initComponent() {
        pager = (PageSlider) findComponentById(ResourceTable.Id_jptab_pager);
        tabBar = (JPTabBar) findComponentById(ResourceTable.Id_text_tabbar);
        jptabback = (Image) findComponentById(ResourceTable.Id_jptabbar_back);
        tabBar.setPageSlider(pager);
        int[] icon = {ResourceTable.Media_nav_01_nor, ResourceTable.Media_nav_02_nor,
            ResourceTable.Media_nav_04_nor, ResourceTable.Media_nav_05_nor};
        int[] iconselect = {ResourceTable.Media_nav_01_pre, ResourceTable.Media_nav_02_pre,
            ResourceTable.Media_nav_04_pre, ResourceTable.Media_nav_05_pre};
        tabBar.createMiddleView(tabmiddleView);
        tabBar.setDatas(mtitle, icon, iconselect, tabmiddleView);
        tabBar.setMiddleItemClickedListener(() -> {
            XToastUtils.toast("中间点击");
        });
        tabBar.setOnClickPageListener(name -> {
            if (isFirst) {
                XToastUtils.toast("点击了" + name);
                isFirst = false;
            }
            XToastUtils.toast("点击了" + name);
            Log.d(TAG, name);
        });
    }
}
